package org.example.user.flight;


import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import static org.example.user.flight.searchBlock.airwayId;

/**
 * 通过起点和终点查询,起点和终点相对应的id需要在air_airway里面查, 信息展示需要展示和展示页面ShowUserFlightInfoView一样的
 * 这个页面需要接收参数,起点和终点
 */
public class searchFlightView extends JFrame {
    private JTable table;
    private JButton buttonAdd, buttonSearch;

    public searchFlightView() {
        setTitle("航班的搜索页面");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout(9, 2));

        // 创建表格模型并设置表格数据
        DefaultTableModel model = new DefaultTableModel();
//        数据测试
//        model.addColumn("航班id"+airwayId);
        model.addColumn("起点");
        model.addColumn("终点");
        model.addColumn("航班id");
        model.addColumn("出发时间");
        model.addColumn("到达时间");
        model.addColumn("飞机模型");
        model.addColumn("航班价格");
        model.addColumn("可预购座位");



        // 连接数据库并获取数据
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:Mysql://localhost:3306/airlines", "root", "123456");
            Statement statement = conn.createStatement();
            //外连接进行三表查询,并使用


            ResultSet resultSet = statement.executeQuery("SELECT \n" +
                    "    air_airway.departure, \n" +
                    "    air_airway.arrival, \n" +
                    "    air_flight.flight_id, \n" +
                    "    air_flight.departure_time, \n" +
                    "    air_flight.arrival_time, \n" +
                    "    air_aircraft.model AS aircraft_model,\n" +
                    "    air_flight.price, \n" +
                    "    air_flight.available_seat \n" +
                    "FROM \n" +
                    "    air_airway\n" +
                    "INNER JOIN\n" +
                    "    air_flight ON air_airway.airway_id = air_flight.airway_id\n" +
                    "INNER JOIN\n" +
                    "    air_aircraft ON air_flight.aircraft_id = air_aircraft.aircraft_id\n" +
                    "WHERE \n" +
                    "    air_airway.airway_id ="+"'"+airwayId+";'");
//            查询起点和终点
//            select * from emp,dept where emp.dep_id=dept.id;

            while (resultSet.next()) {
                String departure = resultSet.getString("departure");
                String arrival = resultSet.getString("arrival");
                int flightId= resultSet.getInt("flight_id");
                String departureTime = resultSet.getString("departure_time");
                String arrivalTime = resultSet.getString("arrival_time");
                String aircraftId = resultSet.getString("aircraft_model");
                String price = resultSet.getString("price");
                String availableSeat = resultSet.getString("available_seat");

                Object[] rowData = {departure,arrival,flightId,departureTime,arrivalTime,aircraftId,price,availableSeat};
                model.addRow(rowData);
            }

            // 关闭连接
            resultSet.close();
            statement.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 创建表格并将模型设置到表格中
        table = new JTable(model);
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane, BorderLayout.CENTER);

        // 添加刷新按钮
        buttonAdd = new JButton("购票");
        JPanel Panel = new JPanel();
        Panel.setLayout(new FlowLayout());
        Panel.add(buttonAdd);
        JLabel jLabel=new JLabel("                                                                  注意: 该表格是按照出发时间升序排列的,购票时需填写航班id");
        jLabel.setSize(200,200);
        Panel.add(jLabel);

        add(Panel, BorderLayout.SOUTH);

        // 添加功能监听器

        buttonAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SwingUtilities.invokeLater(() -> {
                    buyAirTicket buyAirTicketForm = new buyAirTicket();
                    buyAirTicketForm.setSize(380, 260);
                    buyAirTicketForm.setVisible(true);
                });
            }
        });


    }

//    public static void main(String[] args) {
//        SwingUtilities.invokeLater(() -> {
//            searchFlightView searchFlightForm = new searchFlightView();
//            searchFlightForm.setSize(400, 300);
//            searchFlightForm.setVisible(true);
//        });
//    }
}

